##### R CODE TO REPLICATE THE STATISTICAL ANALYSES AND FIGURES OF
##### Adrian Lucardi, Juan Pablo Micozzi and Natan Skigin, "Resignation as Promotion? Executive Turnover and Early Departures in the Argentine Congress, 1983-2017" Forthcoming at Legislative Studies Quarterly

##### PART II: DAILY DATA
##### The following analysis were run with R version 4.0.3 (2020-10-10) -- "Bunny-Wunnies Freak Out"


## emptying the workspace
rm (list=ls ())


## saving the output to a log file
sink (file="Log Analysis Resignation as Promotion 02 - Daily.txt", append=FALSE, type="output", split=FALSE)


## loading the required R packages. This code installs all required packages automativally
if (!require ("pacman")) install.packages ("pacman")
pacman::p_load (
  dplyr
  , lfe
  , tidyr
  , tidyverse
  , xtable)


## display options
options (digits=4
         , scipen=8
         , show.signif.stars=FALSE
         , tibble.width=Inf
         , tibble.print_max=Inf
         , tibble.print_min=1)


## setting working directory --> replace this with path to your own working directory
home <- "~/Dropbox/Current projects/Resignation/"
setwd (home)


## setting the graphical parameters
digits <- 2 ## number of decimals to use in the tables


## function for adding parentheses automatically
parSE <- function (x, n.digits){
  txt <- str_c ("%.", n.digits, "f")
  x.tmp <- matrix (sprintf (as.character (txt), as.matrix (x, n.digits)), ncol=ncol (x), byrow=F)
  x.tmp[seq (2, nrow (x.tmp), by=2),] <- paste ("(", x.tmp[seq (2, nrow (x.tmp), by=2),], ")", sep="")
  x.tmp <- sub ("\\(NA\\)", x.tmp, replacement="")
  x.tmp <- sub ("NA", x.tmp, replacement="")
  return (x.tmp)  }





#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
#### // 1 // DOWNLOADING THE DATA ####
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#

## legislator-day dataset --> 3.1 million obs
load (file="data/Legislator-day full 1983-2017.RData")
summary (lday2); dim (lday2)





#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
#### // 2 // STATISTICAL MODELS ####
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#


###### (2.1) Writing the formula(s) we'll use ######
## to save space; and
## to quickly implement corrections, if needed


## (2.1.1) Main variables

# (2.1.1.1) 30-day window, main formula, no controls
fm01_main30 <- "alternation30_nat_spsp + alternation30_nat_spdp + alternation30_nat_dpdp + alternation30_prov_spsp + alternation30_prov_spdp + alternation30_prov_dpdp"

# (2.1.1.2) 30-day window, including interactions
fm01_position30 <- "alternation30_nat_spsp + I(alternation30_nat_spsp*positionRel) + alternation30_nat_spdp + I(alternation30_nat_spdp*positionRel) + alternation30_nat_dpdp + I(alternation30_nat_dpdp*positionRel) + alternation30_prov_spsp + I(alternation30_prov_spsp*positionRel) + alternation30_prov_spdp + I(alternation30_prov_spdp*positionRel) + alternation30_prov_dpdp + I(alternation30_prov_dpdp*positionRel) + positionRel"

fm01_vulnerable30 <- "alternation30_nat_spsp + I(alternation30_nat_spsp*vulnerable) + alternation30_nat_spdp + I(alternation30_nat_spdp*vulnerable) + alternation30_nat_dpdp + I(alternation30_nat_dpdp*vulnerable) + alternation30_prov_spsp + I(alternation30_prov_spsp*vulnerable) + alternation30_prov_spdp + I(alternation30_prov_spdp*vulnerable) + alternation30_prov_dpdp + I(alternation30_prov_dpdp*vulnerable) + vulnerable"

fm01_exper30 <- "alternation30_nat_spsp + I(alternation30_nat_spsp*any.b) + alternation30_nat_spdp + I(alternation30_nat_spdp*any.b) + alternation30_nat_dpdp + I(alternation30_nat_dpdp*any.b) + alternation30_prov_spsp + I(alternation30_prov_spsp*any.b) + alternation30_prov_spdp + I(alternation30_prov_spdp*any.b) + alternation30_prov_dpdp + I(alternation30_prov_dpdp*any.b) + any.b"

fm01_chair30 <- "alternation30_nat_spsp + I(alternation30_nat_spsp*chair) + alternation30_nat_spdp + I(alternation30_nat_spdp*chair) + alternation30_nat_dpdp + I(alternation30_nat_dpdp*chair) + alternation30_prov_spsp + I(alternation30_prov_spsp*chair) + alternation30_prov_spdp + I(alternation30_prov_spdp*chair) + alternation30_prov_dpdp + I(alternation30_prov_dpdp*chair) + chair"

fm01_midterm30 <- "alternation30_nat_spsp + I(alternation30_nat_spsp*midterm) + alternation30_nat_spdp + I(alternation30_nat_spdp*midterm) + alternation30_nat_dpdp + I(alternation30_nat_dpdp*midterm) + alternation30_prov_spsp + I(alternation30_prov_spsp*midterm) + alternation30_prov_spdp + I(alternation30_prov_spdp*midterm) + alternation30_prov_dpdp + I(alternation30_prov_dpdp*midterm) + midterm"

fm01_gender30 <- "alternation30_nat_spsp + I(alternation30_nat_spsp*female) + alternation30_nat_spdp + I(alternation30_nat_spdp*female) + alternation30_nat_dpdp + I(alternation30_nat_dpdp*female) + alternation30_prov_spsp + I(alternation30_prov_spsp*female) + alternation30_prov_spdp + I(alternation30_prov_spdp*female) + alternation30_prov_dpdp + I(alternation30_prov_dpdp*female) + female"

fm01_party30 <- "alternation30_nat_spsp + I(alternation30_nat_spsp*partyPJ) + I(alternation30_nat_spsp*partyOther) + I(alternation30_nat_spsp*party3rd) + alternation30_nat_spdp + I(alternation30_nat_spdp*partyPJ) + I(alternation30_nat_spdp*partyOther) + I(alternation30_nat_spdp*party3rd) + alternation30_nat_dpdp + I(alternation30_nat_dpdp*partyPJ) + I(alternation30_nat_dpdp*partyOther) + I(alternation30_nat_dpdp*party3rd) + alternation30_prov_spsp + I(alternation30_prov_spsp*partyPJ) + I(alternation30_prov_spsp*partyOther) + I(alternation30_prov_spsp*party3rd) + alternation30_prov_spdp + I(alternation30_prov_spdp*partyPJ) + I(alternation30_prov_spdp*partyOther) + I(alternation30_prov_spdp*party3rd) + alternation30_prov_dpdp + I(alternation30_prov_dpdp*partyPJ) + I(alternation30_prov_dpdp*partyOther) + I(alternation30_prov_dpdp*party3rd) + partyPJ + partyUCR + party3rd" ## baseline is UCR

# (2.1.1.3) 60-day window, main formula, no controls
fm01_main60 <- "alternation60_nat_spsp + alternation60_nat_spdp + alternation60_nat_dpdp + alternation60_prov_spsp + alternation60_prov_spdp + alternation60_prov_dpdp"

# (2.1.1.5) 90-day window, main formula, no controls
fm01_main90 <- "alternation90_nat_spsp + alternation90_nat_spdp + alternation90_nat_dpdp + alternation90_prov_spsp + alternation90_prov_spdp + alternation90_prov_dpdp"


## (2.1.2) controls & extensions: clustered SE's, fixed effects, etc

# (2.1.2.1) all controls
fm02_contall <- "+ female + incGober + incPresi + midterm + voteSh + chair + presid.b + gober.b + sennac.b + dipnac.b + legoth.b + mayor.b + party + log(seats) + positionRel + vulnerable"

# (2.1.2.2) no FE's, clustered SE's
fm03_clname <- "| 0 | 0 | name"

# (2.1.2.3) control FE's, no individual FE's, clustered SE's
fm03_cfeclname <- "| district + period + month_id_period | 0 | name"

# (2.1.2.4) control FE's, individual FE's, clustered SE's
fm03_ifeclname <- "| name + period + month_id_period | 0 | name"

# (2.1.2.5) legislator-mandate FE's, clustered SE's
fm03_lmfeclname <- "| id + month_id_period | 0 | name"




###### (2.2) Day-level analysis ######

### updating the dataset
lday4 <- filter (lday2, elected==1 & assumed==1) %>% select (
  id, name, district, period, date, begDate, end, resignation, death, top01, presid.b, gober.b, sennac.b, dipnac.b, legoth.b, mayor.b, any.b, midterm, female, incGober, incPresi, voteSh, seats, position, positionRel, vulnerable, chair, party, officeNext_full, officeNext_national, officeNext_subnational, officeNext_elected, officeNext_appointed, alternation30_prov, alternation30_nat, alternation60_prov, alternation60_nat, alternation90_prov, alternation90_nat) %>% mutate (
    
    ## multiplying the outcome variables by 100
    resignation100 = resignation*100
    , officeNext_elected100 = officeNext_elected*100
    , officeNext_appointed100 = officeNext_appointed*100
    , death100 = death*100
    
    
    ## party dummies
    , partyPJ = ifelse (party == "PJ", 1, 0)
    , partyUCR = ifelse (party == "UCR", 1, 0) 
    , party3rd = ifelse (party == "3rd party", 1, 0) 
    , partyOther = ifelse (party == "other", 1, 0) 
    
    
    ## creating the treatment dummies
    , alternation30_prov_spsp = ifelse (alternation30_prov=="same party,\nsame person", 1, 0)
    , alternation30_prov_spdp = ifelse (alternation30_prov=="same party,\ndifferent person", 1, 0)
    , alternation30_prov_dpdp = ifelse (alternation30_prov=="different party,\ndifferent person", 1, 0)
    , alternation30_nat_spsp = ifelse (alternation30_nat=="same party,\nsame person", 1, 0)
    , alternation30_nat_spdp = ifelse (alternation30_nat=="same party,\ndifferent person", 1, 0)
    , alternation30_nat_dpdp = ifelse (alternation30_nat=="different party,\ndifferent person", 1, 0)

    , alternation60_prov_spsp = ifelse (alternation60_prov=="same party,\nsame person", 1, 0)
    , alternation60_prov_spdp = ifelse (alternation60_prov=="same party,\ndifferent person", 1, 0)
    , alternation60_prov_dpdp = ifelse (alternation60_prov=="different party,\ndifferent person", 1, 0)
    , alternation60_nat_spsp = ifelse (alternation60_nat=="same party,\nsame person", 1, 0)
    , alternation60_nat_spdp = ifelse (alternation60_nat=="same party,\ndifferent person", 1, 0)
    , alternation60_nat_dpdp = ifelse (alternation60_nat=="different party,\ndifferent person", 1, 0)

    , alternation90_prov_spsp = ifelse (alternation90_prov=="same party,\nsame person", 1, 0)
    , alternation90_prov_spdp = ifelse (alternation90_prov=="same party,\ndifferent person", 1, 0)
    , alternation90_prov_dpdp = ifelse (alternation90_prov=="different party,\ndifferent person", 1, 0)
    , alternation90_nat_spsp = ifelse (alternation90_nat=="same party,\nsame person", 1, 0)
    , alternation90_nat_spdp = ifelse (alternation90_nat=="same party,\ndifferent person", 1, 0)
    , alternation90_nat_dpdp = ifelse (alternation90_nat=="different party,\ndifferent person", 1, 0)

    ## month id's
    , dyear = as.numeric (format (as.Date (date), format="%Y"))
    , dmonth = as.numeric (format (as.Date (date), format="%m")))


## identifying MONTHS with consecutive numbers -> that is, we want a unique identifier for each month: 1 is 1983-12; 2 is 1984-01; 3 is 1984-02, and so on

# getting the unique list of months:
month_id <- unique (select (lday4, dyear, dmonth)) %>% arrange (dyear, dmonth)
month_id$month_id <- 1:nrow (month_id)
summary (month_id); nrow (month_id)

# putting the month identifier into the dataset
lday4 <- left_join (lday4, month_id, by=c ("dyear", "dmonth"))
lday4 <- lday4 %>% group_by (period) %>% mutate (
  month_id_init = min (month_id)
  , month_id_period = month_id - month_id_init)
rm (lday2, month_id)
summary (lday4)



#### (2.2.1) 30-day window, OLS specifications ####

### (2.2.1.0) no interactions

# outcome is resignation
(sum_dols30_all10b <- summary (dols30_all10b <- felm (
  as.formula (paste0 ("resignation100", " ~ ", fm01_main30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))
(sum_dols30_all10c <- summary (dols30_all10c <- felm (
  as.formula (paste0 ("resignation100", " ~ ", fm01_main30, fm02_contall, fm03_ifeclname)), data=filter (lday4, month_id_period >= 2))))
(sum_dols30_all10d <- summary (dols30_all10d <- felm (
  as.formula (paste0 ("resignation100", " ~ ", fm01_main30, fm03_lmfeclname)), data=filter (lday4, month_id_period >= 2))))

# outcome is resignation + elected position
(sum_dols30_all20b <- summary (dols30_all20b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))
(sum_dols30_all20c <- summary (dols30_all20c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main30, fm02_contall, fm03_ifeclname)), data=filter (lday4, month_id_period >= 2))))
(sum_dols30_all20d <- summary (dols30_all20d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main30, fm03_lmfeclname)), data=filter (lday4, month_id_period >= 2))))

# outcome is resignation + appointed position
(sum_dols30_all30b <- summary (dols30_all30b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))
(sum_dols30_all30c <- summary (dols30_all30c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main30, fm02_contall, fm03_ifeclname)), data=filter (lday4, month_id_period >= 2))))
(sum_dols30_all30d <- summary (dols30_all30d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main30, fm03_lmfeclname)), data=filter (lday4, month_id_period >= 2))))

# discarding objects within the model that we don't need but take up too much space
dols30_all10b[c (12:18, 26)] <- NULL; sum_dols30_all10b[c (8, 26)] <- NULL
dols30_all20b[c (12:18, 26)] <- NULL; sum_dols30_all20b[c (8, 26)] <- NULL
dols30_all30b[c (12:18, 26)] <- NULL; sum_dols30_all30b[c (8, 26)] <- NULL
dols30_all10c[c (12:18, 26)] <- NULL; sum_dols30_all10c[c (8, 26)] <- NULL
dols30_all20c[c (12:18, 26)] <- NULL; sum_dols30_all20c[c (8, 26)] <- NULL
dols30_all30c[c (12:18, 26)] <- NULL; sum_dols30_all30c[c (8, 26)] <- NULL
dols30_all10d[c (12:18, 26)] <- NULL; sum_dols30_all10d[c (8, 26)] <- NULL
dols30_all20d[c (12:18, 26)] <- NULL; sum_dols30_all20d[c (8, 26)] <- NULL
dols30_all30d[c (12:18, 26)] <- NULL; sum_dols30_all30d[c (8, 26)] <- NULL



### (2.2.1.1) interacting with top position in party list

# outcome is resignation
(sum_dols30_all11b <- summary (dols30_all11b <- felm (
  as.formula (paste0 ("resignation100", " ~ ", fm01_position30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# outcome is resignation + elected position
(sum_dols30_all21b <- summary (dols30_all21b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_position30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# outcome is resignation + appointed position
(sum_dols30_all31b <- summary (dols30_all31b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_position30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# discarding objects within the model that we don't need but take up too much space
dols30_all11b[c (12:18, 26)] <- NULL; sum_dols30_all11b[c (8, 26)] <- NULL
dols30_all21b[c (12:18, 26)] <- NULL; sum_dols30_all21b[c (8, 26)] <- NULL
dols30_all31b[c (12:18, 26)] <- NULL; sum_dols30_all31b[c (8, 26)] <- NULL



### (2.2.1.2) interacting with vulnerability

# outcome is resignation
(sum_dols30_all12b <- summary (dols30_all12b <- felm (
  as.formula (paste0 ("resignation100", " ~ ", fm01_vulnerable30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# outcome is resignation + elected position
(sum_dols30_all22b <- summary (dols30_all22b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_vulnerable30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# outcome is resignation + appointed position
(sum_dols30_all32b <- summary (dols30_all32b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_vulnerable30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# discarding objects within the model that we don't need but take up too much space
dols30_all12b[c (12:18, 26)] <- NULL; sum_dols30_all12b[c (8, 26)] <- NULL
dols30_all22b[c (12:18, 26)] <- NULL; sum_dols30_all22b[c (8, 26)] <- NULL
dols30_all32b[c (12:18, 26)] <- NULL; sum_dols30_all32b[c (8, 26)] <- NULL



### (2.2.1.3) interacting with previous experience

# outcome is resignation
(sum_dols30_all13b <- summary (dols30_all13b <- felm (
  as.formula (paste0 ("resignation100", " ~ ", fm01_exper30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# outcome is resignation + elected position
(sum_dols30_all23b <- summary (dols30_all23b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_exper30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# outcome is resignation + appointed position
(sum_dols30_all33b <- summary (dols30_all33b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_exper30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# discarding objects within the model that we don't need but take up too much space
dols30_all13b[c (12:18, 26)] <- NULL; sum_dols30_all13b[c (8, 26)] <- NULL
dols30_all23b[c (12:18, 26)] <- NULL; sum_dols30_all23b[c (8, 26)] <- NULL
dols30_all33b[c (12:18, 26)] <- NULL; sum_dols30_all33b[c (8, 26)] <- NULL



### (2.1.1.4) interacting with committee chairmanship

# outcome is resignation
(sum_dols30_all14b <- summary (dols30_all14b <- felm (
  as.formula (paste0 ("resignation100", " ~ ", fm01_chair30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# outcome is resignation + elected position
(sum_dols30_all24b <- summary (dols30_all24b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_chair30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# outcome is resignation + appointed position
(sum_dols30_all34b <- summary (dols30_all34b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_chair30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# discarding objects within the model that we don't need but take up too much space
dols30_all14b[c (12:18, 26)] <- NULL; sum_dols30_all14b[c (8, 26)] <- NULL
dols30_all24b[c (12:18, 26)] <- NULL; sum_dols30_all24b[c (8, 26)] <- NULL
dols30_all34b[c (12:18, 26)] <- NULL; sum_dols30_all34b[c (8, 26)] <- NULL



### (2.2.1.5) interacting with election in midterm

# outcome is resignation
(sum_dols30_all15b <- summary (dols30_all15b <- felm (
  as.formula (paste0 ("resignation100", " ~ ", fm01_midterm30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# outcome is resignation + elected position
(sum_dols30_all25b <- summary (dols30_all25b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_midterm30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# outcome is resignation + appointed position
(sum_dols30_all35b <- summary (dols30_all35b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_midterm30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# discarding objects within the model that we don't need but take up too much space
dols30_all15b[c (12:18, 26)] <- NULL; sum_dols30_all15b[c (8, 26)] <- NULL
dols30_all25b[c (12:18, 26)] <- NULL; sum_dols30_all25b[c (8, 26)] <- NULL
dols30_all35b[c (12:18, 26)] <- NULL; sum_dols30_all35b[c (8, 26)] <- NULL



### (2.2.1.6) interacting with gender

# outcome is resignation
(sum_dols30_all16b <- summary (dols30_all16b <- felm (
  as.formula (paste0 ("resignation100", " ~ ", fm01_gender30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# outcome is resignation + elected position
(sum_dols30_all26b <- summary (dols30_all26b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_gender30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# outcome is resignation + appointed position
(sum_dols30_all36b <- summary (dols30_all36b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_gender30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# discarding objects within the model that we don't need but take up too much space
dols30_all16b[c (12:18, 26)] <- NULL; sum_dols30_all16b[c (8, 26)] <- NULL
dols30_all26b[c (12:18, 26)] <- NULL; sum_dols30_all26b[c (8, 26)] <- NULL
dols30_all36b[c (12:18, 26)] <- NULL; sum_dols30_all36b[c (8, 26)] <- NULL



### (2.2.1.7) interacting with party ID

# outcome is resignation
(sum_dols30_all17b <- summary (dols30_all17b <- felm (
  as.formula (paste0 ("resignation100", " ~ ", fm01_party30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# outcome is resignation + elected position
(sum_dols30_all27b <- summary (dols30_all27b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_party30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# outcome is resignation + appointed position
(sum_dols30_all37b <- summary (dols30_all37b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_party30, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 2))))

# discarding objects within the model that we don't need but take up too much space
dols30_all17b[c (12:18, 26)] <- NULL; sum_dols30_all17b[c (8, 26)] <- NULL
dols30_all27b[c (12:18, 26)] <- NULL; sum_dols30_all27b[c (8, 26)] <- NULL
dols30_all37b[c (12:18, 26)] <- NULL; sum_dols30_all37b[c (8, 26)] <- NULL



#### (2.2.2) 60-day window, OLS specifications ####

### (2.2.2.0) no interactions

# outcome is resignation
(sum_dols60_all10b <- summary (dols60_all10b <- felm (
  as.formula (paste0 ("resignation100", " ~ ", fm01_main60, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 3))))
(sum_dols60_all10c <- summary (dols60_all10c <- felm (
  as.formula (paste0 ("resignation100", " ~ ", fm01_main60, fm02_contall, fm03_ifeclname)), data=filter (lday4, month_id_period >= 3))))
(sum_dols60_all10d <- summary (dols60_all10d <- felm (
  as.formula (paste0 ("resignation100", " ~ ", fm01_main60, fm03_lmfeclname)), data=filter (lday4, month_id_period >= 3))))

# outcome is resignation + elected position
(sum_dols60_all20b <- summary (dols60_all20b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main60, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 3))))
(sum_dols60_all20c <- summary (dols60_all20c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main60, fm02_contall, fm03_ifeclname)), data=filter (lday4, month_id_period >= 3))))
(sum_dols60_all20d <- summary (dols60_all20d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main60, fm03_lmfeclname)), data=filter (lday4, month_id_period >= 3))))

# outcome is resignation + appointed position
(sum_dols60_all30b <- summary (dols60_all30b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main60, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 3))))
(sum_dols60_all30c <- summary (dols60_all30c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main60, fm02_contall, fm03_ifeclname)), data=filter (lday4, month_id_period >= 3))))
(sum_dols60_all30d <- summary (dols60_all30d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main60, fm03_lmfeclname)), data=filter (lday4, month_id_period >= 3))))

# discarding objects within the model that we don't need but take up too much space
dols60_all10b[c (12:18, 26)] <- NULL; sum_dols60_all10b[c (8, 26)] <- NULL
dols60_all20b[c (12:18, 26)] <- NULL; sum_dols60_all20b[c (8, 26)] <- NULL
dols60_all30b[c (12:18, 26)] <- NULL; sum_dols60_all30b[c (8, 26)] <- NULL
dols60_all10c[c (12:18, 26)] <- NULL; sum_dols60_all10c[c (8, 26)] <- NULL
dols60_all20c[c (12:18, 26)] <- NULL; sum_dols60_all20c[c (8, 26)] <- NULL
dols60_all30c[c (12:18, 26)] <- NULL; sum_dols60_all30c[c (8, 26)] <- NULL
dols60_all10d[c (12:18, 26)] <- NULL; sum_dols60_all10d[c (8, 26)] <- NULL
dols60_all20d[c (12:18, 26)] <- NULL; sum_dols60_all20d[c (8, 26)] <- NULL
dols60_all30d[c (12:18, 26)] <- NULL; sum_dols60_all30d[c (8, 26)] <- NULL




#### (2.2.3) 90-day window, OLS specifications ####

### (2.2.3.0) no interactions

# outcome is resignation
(sum_dols90_all10b <- summary (dols90_all10b <- felm (
  as.formula (paste0 ("resignation100", " ~ ", fm01_main90, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 4))))
(sum_dols90_all10c <- summary (dols90_all10c <- felm (
  as.formula (paste0 ("resignation100", " ~ ", fm01_main90, fm02_contall, fm03_ifeclname)), data=filter (lday4, month_id_period >= 4))))
(sum_dols90_all10d <- summary (dols90_all10d <- felm (
  as.formula (paste0 ("resignation100", " ~ ", fm01_main90, fm03_lmfeclname)), data=filter (lday4, month_id_period >= 4))))

# outcome is resignation + elected position
(sum_dols90_all20b <- summary (dols90_all20b <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main90, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 4))))
(sum_dols90_all20c <- summary (dols90_all20c <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main90, fm02_contall, fm03_ifeclname)), data=filter (lday4, month_id_period >= 4))))
(sum_dols90_all20d <- summary (dols90_all20d <- felm (
  as.formula (paste0 ("officeNext_elected100", " ~ ", fm01_main90, fm03_lmfeclname)), data=filter (lday4, month_id_period >= 4))))

# outcome is resignation + appointed position
(sum_dols90_all30b <- summary (dols90_all30b <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main90, fm02_contall, fm03_cfeclname)), data=filter (lday4, month_id_period >= 4))))
(sum_dols90_all30c <- summary (dols90_all30c <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main90, fm02_contall, fm03_ifeclname)), data=filter (lday4, month_id_period >= 4))))
(sum_dols90_all30d <- summary (dols90_all30d <- felm (
  as.formula (paste0 ("officeNext_appointed100", " ~ ", fm01_main90, fm03_lmfeclname)), data=filter (lday4, month_id_period >= 4))))

# discarding objects within the model that we don't need but take up too much space
dols90_all10b[c (12:18, 26)] <- NULL; sum_dols90_all10b[c (8, 26)] <- NULL
dols90_all20b[c (12:18, 26)] <- NULL; sum_dols90_all20b[c (8, 26)] <- NULL
dols90_all30b[c (12:18, 26)] <- NULL; sum_dols90_all30b[c (8, 26)] <- NULL
dols90_all10c[c (12:18, 26)] <- NULL; sum_dols90_all10c[c (8, 26)] <- NULL
dols90_all20c[c (12:18, 26)] <- NULL; sum_dols90_all20c[c (8, 26)] <- NULL
dols90_all30c[c (12:18, 26)] <- NULL; sum_dols90_all30c[c (8, 26)] <- NULL
dols90_all10d[c (12:18, 26)] <- NULL; sum_dols90_all10d[c (8, 26)] <- NULL
dols90_all20d[c (12:18, 26)] <- NULL; sum_dols90_all20d[c (8, 26)] <- NULL
dols90_all30d[c (12:18, 26)] <- NULL; sum_dols90_all30d[c (8, 26)] <- NULL





#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
#### // 3 // REPORTING THE RESULTS ####
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#

###### (3.1) Unconditional effects ######

## adding up the outcomes
resigs <- na.omit (select (filter (lday4, month_id_period >= 2), resignation100
                           , alternation30_nat_spsp, alternation30_nat_spdp, alternation30_nat_dpdp, alternation30_prov_spsp, alternation30_prov_spdp, alternation30_prov_dpdp, female, incGober, incPresi, midterm, voteSh, chair, presid.b, gober.b, sennac.b, dipnac.b, legoth.b, mayor.b, party, seats, positionRel, vulnerable, district, period, month_id_period, name))$resignation100
resig_ele <- na.omit (select (filter (lday4, month_id_period >= 2), officeNext_elected100
                              , alternation30_nat_spsp, alternation30_nat_spdp, alternation30_nat_dpdp, alternation30_prov_spsp, alternation30_prov_spdp, alternation30_prov_dpdp, female, incGober, incPresi, midterm, voteSh, chair, presid.b, gober.b, sennac.b, dipnac.b, legoth.b, mayor.b, party, seats, positionRel, vulnerable, district, period, month_id_period, name))$officeNext_elected100
resig_appt <- na.omit (select (filter (lday4, month_id_period >= 2), officeNext_appointed100
                               , alternation30_nat_spsp, alternation30_nat_spdp, alternation30_nat_dpdp, alternation30_prov_spsp, alternation30_prov_spdp, alternation30_prov_dpdp, female, incGober, incPresi, midterm, voteSh, chair, presid.b, gober.b, sennac.b, dipnac.b, legoth.b, mayor.b, party, seats, positionRel, vulnerable, district, period, month_id_period, name))$officeNext_appointed100
resigs <- sum (resigs/100)
resig_ele <- sum (resig_ele/100)
resig_appt <- sum (resig_appt/100)


## constructing a matrix with the values, which we will later format for Latex
(tabDay1coef <- bind_rows (
  
  # 30-day interval
  bind_cols (sum_dols30_all10b$coef[1:6,1], sum_dols30_all10c$coef[1:6,1], sum_dols30_all10d$coef[1:6,1], rep (NA, 6)
             , sum_dols30_all20b$coef[1:6,1], sum_dols30_all20c$coef[1:6,1], sum_dols30_all20d$coef[1:6,1], rep (NA, 6)
             , sum_dols30_all30b$coef[1:6,1], sum_dols30_all30c$coef[1:6,1], sum_dols30_all30d$coef[1:6,1])
  
  # 60-day interval
  , bind_cols (sum_dols60_all10b$coef[1:6,1], sum_dols60_all10c$coef[1:6,1], sum_dols60_all10d$coef[1:6,1], rep (NA, 6)
               , sum_dols60_all20b$coef[1:6,1], sum_dols60_all20c$coef[1:6,1], sum_dols60_all20d$coef[1:6,1], rep (NA, 6)
               , sum_dols60_all30b$coef[1:6,1], sum_dols60_all30c$coef[1:6,1], sum_dols60_all30d$coef[1:6,1])
  
  # 90-day interval
  , bind_cols (sum_dols90_all10b$coef[1:6,1], sum_dols90_all10c$coef[1:6,1], sum_dols90_all10d$coef[1:6,1], rep (NA, 6)
               , sum_dols90_all20b$coef[1:6,1], sum_dols90_all20c$coef[1:6,1], sum_dols90_all20d$coef[1:6,1], rep (NA, 6)
               , sum_dols90_all30b$coef[1:6,1], sum_dols90_all30c$coef[1:6,1], sum_dols90_all30d$coef[1:6,1]) ))
(tabDay1se <- bind_rows (
  
  # 30-day interval
  bind_cols (sum_dols30_all10b$coef[1:6,2], sum_dols30_all10c$coef[1:6,2], sum_dols30_all10d$coef[1:6,2], rep (NA, 6)
             , sum_dols30_all20b$coef[1:6,2], sum_dols30_all20c$coef[1:6,2], sum_dols30_all20d$coef[1:6,2], rep (NA, 6)
             , sum_dols30_all30b$coef[1:6,2], sum_dols30_all30c$coef[1:6,2], sum_dols30_all30d$coef[1:6,2])
  
  # 60-day interval
  , bind_cols (sum_dols60_all10b$coef[1:6,2], sum_dols60_all10c$coef[1:6,2], sum_dols60_all10d$coef[1:6,2], rep (NA, 6)
               , sum_dols60_all20b$coef[1:6,2], sum_dols60_all20c$coef[1:6,2], sum_dols60_all20d$coef[1:6,2], rep (NA, 6)
               , sum_dols60_all30b$coef[1:6,2], sum_dols60_all30c$coef[1:6,2], sum_dols60_all30d$coef[1:6,2])
  
  # 90-day interval
  , bind_cols (sum_dols90_all10b$coef[1:6,2], sum_dols90_all10c$coef[1:6,2], sum_dols90_all10d$coef[1:6,2], rep (NA, 6)
               , sum_dols90_all20b$coef[1:6,2], sum_dols90_all20c$coef[1:6,2], sum_dols90_all20d$coef[1:6,2], rep (NA, 6)
               , sum_dols90_all30b$coef[1:6,2], sum_dols90_all30c$coef[1:6,2], sum_dols90_all30d$coef[1:6,2]) ))

# creating the table
(tabDay1all <- parSE (bind_rows (tabDay1coef, tabDay1se)[sort (rep (1:18, 2)) + rep (c (0, 18), 2),], n.digit=digits+1))

# formatting for Latex
rows <- rep (c ("national:", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
                , "national:", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
                , "national:", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"
                , "[0.50ex] provincial:", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
                , "provincial:", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
                , "provincial:", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"), 3)
(tabDay1ns <- c (resigs, resig_ele, resig_appt))

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\emph{resignation}} & & \\multicolumn{3}{c}{\\emph{resignation \\& election}} & & \\multicolumn{3}{c}{\\emph{resignation \\& app'tment}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header2 <- paste ("\\multicolumn{1}{l}{(a) 30-day window} & \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} & \\multicolumn{1}{c}{(3)} & & \\multicolumn{1}{c}{(4)} & \\multicolumn{1}{c}{(5)} & \\multicolumn{1}{c}{(6)} & & \\multicolumn{1}{c}{(7)} & \\multicolumn{1}{c}{(8)} & \\multicolumn{1}{c}{(9)} \\\\ \\midrule \n")
Header3 <- paste ("[1.25ex] \\multicolumn{12}{l}{(b) 60-day window} \\\\ \\midrule \n")
Header4 <- paste ("[1.25ex] \\multicolumn{12}{l}{(c) 90-day window} \\\\ \\midrule \n")

add.n03 <- paste ("[0.20ex] legislator \\textsc{fe}'s & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{\\textsc{x}} & \\multicolumn{1}{c}{} & & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{\\textsc{x}} & \\multicolumn{1}{c}{} & & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{\\textsc{x}} & \\multicolumn{1}{c}{} \\\\ \n")
add.n04 <- paste ("[0.20ex] leg.-mandate \\textsc{fe}'s & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{\\textsc{x}} & & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{\\textsc{x}} & & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{\\textsc{x}} \\\\ \n")

add.n05 <- paste ("\\midrule $\\sum Y = 1$ & \\multicolumn{3}{c}{", sprintf ("%.0f", round (tabDay1ns[1],0)), "} & & \\multicolumn{3}{c}{", sprintf ("%.0f", round (tabDay1ns[2],0)), "} & & \\multicolumn{3}{c}{", sprintf ("%.0f", round (tabDay1ns[3],0)), "} \\\\ \n")
add.n06 <- paste ("[0.20ex] num. legislators & \\multicolumn{3}{c}{", sprintf ("%.0f", round (length (unique (lday4$name)),0)), "} & & \\multicolumn{3}{c}{", sprintf ("%.0f", round (length (unique (lday4$name)),0)), "} & & \\multicolumn{3}{c}{", sprintf ("%.0f", round (length (unique (lday4$name)),0)), "} \\\\ \n")
add.n07 <- paste ("[0.20ex] num. mandates & \\multicolumn{3}{c}{", sprintf ("%.0f", round (length (unique (lday4$id)),0)), "} & & \\multicolumn{3}{c}{", sprintf ("%.0f", round (length (unique (lday4$id)),0)), "} & & \\multicolumn{3}{c}{", sprintf ("%.0f", round (length (unique (lday4$id)),0)), "} \\\\ \n")
add.n08 <- paste ("[0.20ex] num. obs. & \\multicolumn{3}{c}{", sprintf ("%.0f", round (dols30_all10b$N,0)), "} & & \\multicolumn{3}{c}{", sprintf ("%.0f", round (dols30_all20b$N,0)), "} & & \\multicolumn{3}{c}{", sprintf ("%.0f", round (dols30_all30b$N,0)), "} \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 12
addtorow$pos[[4]] <- 24
addtorow$pos[[5]] <- 36
addtorow$pos[[6]] <- 36
addtorow$pos[[7]] <- 36
addtorow$command <- c (Header1, Header2, Header3, Header4
                       , add.n03, add.n04, add.n05)
print (xtable ( cbind (rows, tabDay1all)
                , align=c ('l','l','c','c','c','c','c','c','c','c','c','c','c')
                , digits=digits
                , caption="Executive alternation and legislative resignation in Argentina, 1983-2017 (Daily data)"
                , label="T:resultsDailyMain")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow )

# getting the number of observations to report at the bottom of the table
round (length (unique (filter (lday4, month_id_period >= 4)$name)));round (length (unique (filter (lday4, month_id_period >= 4)$id))); sum_dols90_all10b$N ## 1592; 2111; 2579802
round (length (unique (filter (lday4, month_id_period >= 3)$name)));round (length (unique (filter (lday4, month_id_period >= 3)$id))); sum_dols60_all10b$N ## 1596; 2117; 2645354
round (length (unique (filter (lday4, month_id_period >= 2)$name)));round (length (unique (filter (lday4, month_id_period >= 2)$id))); sum_dols30_all10b$N ## 1598; 2120; 2705845



#### (3.2) Main interaction: by Day, 30-day window, only OLS models ######

## constructing a matrix with the values, which we will later format for Latex
(tabDay2coef <- bind_cols (
  sum_dols30_all11b$coef[1:12,1], sum_dols30_all21b$coef[1:12,1], sum_dols30_all31b$coef[1:12,1], rep (NA, 12)
  , sum_dols30_all12b$coef[1:12,1], sum_dols30_all22b$coef[1:12,1], sum_dols30_all32b$coef[1:12,1], rep (NA, 12)
  , sum_dols30_all13b$coef[1:12,1], sum_dols30_all23b$coef[1:12,1], sum_dols30_all33b$coef[1:12,1], rep (NA, 12)
  , sum_dols30_all14b$coef[1:12,1], sum_dols30_all24b$coef[1:12,1], sum_dols30_all34b$coef[1:12,1], rep (NA, 12)
  , sum_dols30_all15b$coef[1:12,1], sum_dols30_all25b$coef[1:12,1], sum_dols30_all35b$coef[1:12,1], rep (NA, 12)
  , sum_dols30_all16b$coef[1:12,1], sum_dols30_all26b$coef[1:12,1], sum_dols30_all36b$coef[1:12,1], rep (NA, 12)
  , sum_dols30_all17b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1], sum_dols30_all27b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1], sum_dols30_all37b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),1]))
(tabDay2se <- bind_cols (
  sum_dols30_all11b$coef[1:12,2], sum_dols30_all21b$coef[1:12,2], sum_dols30_all31b$coef[1:12,2], rep (NA, 12)
  , sum_dols30_all12b$coef[1:12,2], sum_dols30_all22b$coef[1:12,2], sum_dols30_all32b$coef[1:12,2], rep (NA, 12)
  , sum_dols30_all13b$coef[1:12,2], sum_dols30_all23b$coef[1:12,2], sum_dols30_all33b$coef[1:12,2], rep (NA, 12)
  , sum_dols30_all14b$coef[1:12,2], sum_dols30_all24b$coef[1:12,2], sum_dols30_all34b$coef[1:12,2], rep (NA, 12)
  , sum_dols30_all15b$coef[1:12,2], sum_dols30_all25b$coef[1:12,2], sum_dols30_all35b$coef[1:12,2], rep (NA, 12)
  , sum_dols30_all16b$coef[1:12,2], sum_dols30_all26b$coef[1:12,2], sum_dols30_all36b$coef[1:12,2], rep (NA, 12)
  , sum_dols30_all17b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2], sum_dols30_all27b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2], sum_dols30_all37b$coef[c (1:2, 5:6, 9:10, 13:14, 17:18, 21:22),2]))

## creating the table
(tabDay2all <- parSE (bind_rows (tabDay2coef, tabDay2se)[sort (rep (1:12, 2)) + rep (c (0, 12), 2),], n.digit=digits))

## formatting for Latex
rows <- c ("national (30-day):", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "national (30-day):", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "national (30-day):", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "[1.5ex] provincial (30-day):", "~~~~$=$ \\emph{party,} $=$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "provincial (30-day):", "~~~~$=$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , ""
           , "provincial (30-day):", "~~~~$\\neq$ \\emph{party,} $\\neq$ \\emph{person}"
           , "~~$\\times$ conditional" , "")
(tabDay2ns <- rep (c (resigs, resig_ele, resig_appt), 7))

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{position in list}} & & \\multicolumn{3}{c}{\\textbf{elect. vulnerable}} & & \\multicolumn{3}{c}{\\textbf{previous experience}} & & \\multicolumn{3}{c}{\\textbf{committee chair}} & & \\multicolumn{3}{c}{\\textbf{elected in midterm}} & & \\multicolumn{3}{c}{\\textbf{female}} & & \\multicolumn{3}{c}{\\textbf{party (\\textsc{ucr}/\\textsc{pj})}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \\cmidrule{14-16} \\cmidrule{18-20} \\cmidrule{22-24} \\cmidrule{26-28} \n")
Header2 <- paste ("& \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} & \\multicolumn{1}{c}{(3)} & & \\multicolumn{1}{c}{(4)} & \\multicolumn{1}{c}{(5)} & \\multicolumn{1}{c}{(6)} & & \\multicolumn{1}{c}{(7)} & \\multicolumn{1}{c}{(8)} & \\multicolumn{1}{c}{(9)} & & \\multicolumn{1}{c}{(10)} & \\multicolumn{1}{c}{(11)} & \\multicolumn{1}{c}{(12)} & & \\multicolumn{1}{c}{(13)} & \\multicolumn{1}{c}{(14)} & \\multicolumn{1}{c}{(15)} & & \\multicolumn{1}{c}{(16)} & \\multicolumn{1}{c}{(17)} & \\multicolumn{1}{c}{(18)} & & \\multicolumn{1}{c}{(19)} & \\multicolumn{1}{c}{(20)} & \\multicolumn{1}{c}{(21)} \\\\ \\midrule \n")

add.n01 <- paste ("\\midrule outcome & \\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} & &
\\multicolumn{1}{c}{\\textsc{r}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{e}} & \\multicolumn{1}{c}{\\textsc{r}\\&\\textsc{a}} \\\\ \n")
add.n02 <- paste ("$\\sum Y = 1$ & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[2],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[3],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[4],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[5],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[6],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[7],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[8],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[9],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[10],0)),"} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[11],0)),"} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[12],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[13],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[14],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[15],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[16],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[17],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[18],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[19],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[20],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (tabDay2ns[21],0)), "} \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 24
addtorow$pos[[4]] <- 24
addtorow$command <- c (Header1, Header2, add.n01, add.n02)
print (xtable ( cbind (rows, tabDay2all)
                , align=c ('l','l','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c','c')
                , digits=digits
                , caption="Executive alternation and legislative resignation in Argentina, 1983-2017: Conditional effects (Daily data)"
                , label="T:resultsInt30")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="scriptsize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow )

# getting the number of observations to report at the bottom of the table
round (length (unique (filter (lday4, month_id_period >= 2)$name)));round (length (unique (filter (lday4, month_id_period >= 2)$id))); sum_dols30_all11b$N ## 1598; 2120; 2705845




#### Saving --> if you want to access the results later, uncomment and run this code ####

# ## setting the working directory
# setwd (home)
# 
# system.time (save.image ("Results Resignations Argentina (daily).RData"))
# system.time (load ("Results Resignations Argentina (daily).RData"))

sink (NULL)
